package com.ken.spring.beans;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

@Repository
public class UserDao {
    @Autowired
    JdbcTemplate jdbcTemplate;

    public User getUser(Integer id) {
        String sql = "SELECT id,password, username,address,userId from user where id = ?";
        RowMapper<User> rowMapper = new BeanPropertyRowMapper<>(User.class);
        return jdbcTemplate.queryForObject(sql, rowMapper, id);
    }

    /**
     * 更新用户账户余额
     */
    public void updateUserAccountById(String username, Float account) {

        String sql = "UPDATE user set account = ?-1 where username = ? ";
        int i = jdbcTemplate.update(sql, account, username);
        System.out.println("更新用户账户余额->" + i);
    }

}
